package com.kyrie.controller;

import com.kyrie.dto.GetDictionariesDto;
import com.kyrie.dto.OperateDictionariesDto;
import com.kyrie.system.druid.DatasourceSelectHelper;
import com.kyrie.system.mybatis.execption.ServiceException;
import com.kyrie.utils.GlobalConstants;
import com.kyrie.utils.OperationCommUtil;
import com.kyrie.vo.ResponseBean;
import com.kyrie.vo.TOperationCommTypeVal;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/***
 * 描述: 数据字典
 *
 * @author wuxiang
 * @date 2020-04-07 23:50
 */
@Validated
@RestController
@RequestMapping("/commVal")
public class TOperationController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "operationCommUtil")
    private OperationCommUtil operationCommUtil;

    /**
     * 获取数据字典列表
     */
    @RequestMapping(value = "/getDictionaries", method = RequestMethod.POST)
    @RequiresRoles(logical = Logical.OR, value = {"systemAdmin"})
    public ResponseBean<List<TOperationCommTypeVal>> getDictionaries(@Valid @RequestBody GetDictionariesDto getDictionariesDto) throws ServiceException {
        String typeId = getDictionariesDto.getTypeId();
        String valId = getDictionariesDto.getValId();
        List<TOperationCommTypeVal> list = operationCommUtil.getDictList(typeId,valId);
        return ResponseBean.success(list, GlobalConstants.SUCCESS_CODE, "@数据字典查询成功");
    }

    /**
     * 修改+新增+删除数据字典
     */
    @RequestMapping(value = "/operateDictionaries", method = RequestMethod.POST)
    @RequiresRoles(logical = Logical.OR, value = {"systemAdmin"})
    public ResponseBean<List<TOperationCommTypeVal>> operateDictionaries(@Valid @RequestBody OperateDictionariesDto operateDictionariesDto) throws ServiceException {
        DatasourceSelectHelper.setKey(DatasourceSelectHelper.CONFIG_DB);
        String operateType = operateDictionariesDto.getOperateType();
        switch (operateType) {
            case "add":
                // 新增字典
                operationCommUtil.addDictonaries(operateDictionariesDto);
                return ResponseBean.success(null, GlobalConstants.SUCCESS_CODE, "@数据字典添加成功！");
            case "delete":
                // 删除字典
                operationCommUtil.delDictonaries(operateDictionariesDto);
                return ResponseBean.success(null, GlobalConstants.SUCCESS_CODE, "@数据字典删除成功！");
            case "update":
                // 更新字典
                operationCommUtil.updDictonaries(operateDictionariesDto);
                return ResponseBean.success(null, GlobalConstants.SUCCESS_CODE, "@数据字典更新成功！");
            default:
                // 未匹配到操作类型
                return ResponseBean.fail(null, GlobalConstants.FAIL_CODE, "@数据字典操作失败，失败原因：字典操作类型：" + operateType + "非法！");
        }
    }
}
